<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="80%" destroy-on-close @closed="$emit('closed')" align-center>
        <el-container>
		<el-aside width="15%" v-loading="menuloading">
			<el-container>
                <el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="menuFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="menu" class="menu" node-key="id" :data="menuList" :props="menuProps" :current-node-key="''"
							 draggable highlight-current :expand-on-click-node="false"
							 :filter-node-method="menuFilterNode" @node-click="menuClick"></el-tree>
				</el-main>
            </el-container>
		</el-aside>
		<el-container>
			<el-header>
                <div class="left-panel">
                    <el-button type="primary" @click="back">确定</el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="add">添加物料</el-button>
                </div>
                <div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keywords" placeholder="请输入关键词" clearable @change="handleKeyDown"></el-input>
                        <el-select style="margin:5px 10px" v-model="search.unit_id" filterable clearable placeholder="请选择单位">
                            <el-option v-for="item in unitList" :key="item.id" :label="item.unit_name" :value="item.id"/>
                        </el-select>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                        <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
					</div>
				</div>
            </el-header>
			<el-main class="nopadding">
				<scTable ref="table" row-key="id" @select-all="selectAll" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border height="600">
					<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                    <el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
					<el-table-column label="物料编号" prop="goods_code" align="center"></el-table-column>
					<el-table-column label="物料名称" prop="goods_name" align="center"></el-table-column>
					<el-table-column label="物料分类" prop="cate_name" align="center"></el-table-column>
                    <el-table-column label="计量单位" prop="unit_name" align="center"></el-table-column>
					<el-table-column label="规格型号" prop="goods_spec" align="center"></el-table-column>
					<el-table-column label="购买方式" prop="pay_type" align="center">
                        <template #default="scope">
                            <el-tag v-if="scope.row.pay_type == 1" type="success">自制</el-tag>
                            <el-tag v-if="scope.row.pay_type == 2" type="primary">外购</el-tag>
                        </template>
                    </el-table-column>
					<el-table-column label="可销售" prop="is_sale" align="center">
                        <template #default="scope">
                            <el-tag v-if="scope.row.is_sale == 1" type="success">可销售</el-tag>
                            <el-tag v-if="scope.row.is_sale == 0" type="danger">不可销售</el-tag>
                        </template>
                    </el-table-column>
					<el-table-column label="可采购" prop="is_pro" align="center">
                        <template #default="scope">
                            <el-tag v-if="scope.row.is_pro == 1" type="success">可采购</el-tag>
                            <el-tag v-if="scope.row.is_pro == 0" type="danger">不可采购</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="物料图片" align="center">
						<template #default="scope">
							<el-image v-if="scope.row.goods_image!=''" style="width: 50px; height: 50px; border-radius: 5px;" :src="scope.row.goods_image[0]" :preview-src-list="scope.row.goods_image" preview-teleported="true" hide-on-click-modal align="center"></el-image>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	    </el-container>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
		</template>
        <save-add v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-add>
	</el-dialog>
</template>

<script>
import saveAdd from './save.vue'
export default {
  components: { saveAdd },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '选择物料',
				route: '选择物料',
				edit: '编辑物料',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			selection: [],
            apiObj: this.$API.device.beiJian.goods.lists,
			search: {
				keywords: null,
				cate_id: null,
                unit_id:null,
			},
            menuList: [],
			menuProps: {
				label: (data) => {
					return data.cate_name
				}
			},
			menuloading: false,
			menuFilterText: "",
			menuForm: {},
            showGrouploading:"",
            dialog: {
				save: false,
			},
            unitList:[],
            index:"",
		}
	},
	mounted() {
        this.getCate();
        this.getUnit();
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		// 表单注入数据
		setData(data) {
            this.index = data
		},
        //加载树数据
		async getCate() {
			this.showGrouploading = true;
			var res = await this.$API.all.getBeiCate.get();
			this.showGrouploading = false;
			var allNode = { id: '', cate_name: '所有' }
			res.data.unshift(allNode);
			this.menuList = res.data;
            this.menuClick(allNode)
		},
        //树点击
		menuClick(data) {
			this.menuForm = data
			var params = {
				cate_id: data.id,
			}
			this.$refs.table.reload(params)
		},
		//树过滤
		menuFilterNode(value, data) {
			if (!value) return true;
			return data.title.indexOf(value) !== -1;
		},
        // 选择事件
        selectionChange(selection) {
            if(this.mode == 'bom' || this.mode == 'route'){
                if(selection.length > 1){
                    const del_row = selection.shift()
                    this.$refs.table.toggleRowSelection(del_row,false) //设置这一行取消选中
                }
            }
			this.selection = selection;
		},
        selectAll() {
            if(this.mode == 'bom' || this.mode == 'route'){
                this.$message.error("只能单选！")
			    this.$refs.table.clearSelection()
            }
		},
        // 选中返回
        back(){
            this.$emit('success', this.selection,this.mode,this.index)
            this.visible = false;
        },
        //客户新增
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
        // 更新客户新增
        handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        //搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        // 重置
        clearSearch(){
            this.search.text_select = null
        },
        async getUnit(){
            var res = await this.$API.all.getBeiUnit.get();
            this.unitList = res.data
        },
	}
}
</script>

<style></style>
